﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Data.SqlClient;

namespace hozoroghiab
{
    public partial class absents : Form
    {
        public sql sqlquery;
        int[] month = new int[12] { 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29 };
        public absents(sql s)
        {
            InitializeComponent();
            sqlquery = s;
        }

        private void dayly_absent_Load(object sender, EventArgs e)
        {
            PersianCalendar per = new PersianCalendar();
            string year = per.GetYear(DateTime.Now).ToString();
            string month = per.GetMonth(DateTime.Now).ToString();
            setdatagridview(year, month);
            textBox1.Text = year;
            textBox2.Text = month;
        }

        private void setdatagridview(string year, string month)
        {
            sqlquery.open();
            int count = (int)sqlquery.ExecuteScalar("SELECT COUNT(*) FROM days where year=" + year + " and month=" + month + "");
            PersianCalendar per = new PersianCalendar();
            int[] monthcopy = new int[12];
            Array.Copy(this.month, monthcopy, this.month.Length);
            if (per.IsLeapYear(Convert.ToInt32(year)))
            {
                monthcopy[11] = 30;
            }

            if (count != monthcopy[Convert.ToInt16(month) - 1])
            {
                string value = "";
                for (int i = 0; i < monthcopy[Convert.ToInt16(month) - 1]; i++)
                {
                    value += "(" + year + "," + month + "," + i + "),";
                }
                sqlquery.ExecuteNonQuery("INSERT INTO days (year,month,day)VALUES" + value.Substring(0, value.Length - 1));
            }
            SqlDataAdapter da = sqlquery.sqlcommand("SELECT * FROM days where year=" + year + " and month=" + month + "");
            DataTable table = new DataTable();
            da.Fill(table);
            dataGridView1.DataSource = table;
            sqlquery.close();
        }

        private void ok_btn_Click_1(object sender, EventArgs e)
        {
            setdatagridview(textBox1.Text.Trim(), textBox2.Text.Trim());
        }

        private void ok_btn2_Click(object sender, EventArgs e)
        {
            setdatagridview(textBox1.Text.Trim(), textBox2.Text.Trim());
        }

        
    }
}
